<template>
  <div class="coffee_machineIndex">
    <c-title :hide="false" text="门店中心"></c-title>
    <div class="headBox">
      <div class="left">
        <div class="title mb">
          <span>设备名称：</span>
          <span v-if="!editShow">{{ name }}</span>
          <input type="text" v-model="name" @keydown.enter="blurInp" @blur="blurInp" v-if="editShow" ref="nameInp" />
          <i class="iconfont icon-fontclass-qianshu" @click="editTap"></i>
        </div>
        <div class="title sm">设备号：{{ info.code }}</div>
      </div>
      <div class="right">
        <div class="btn">ID: {{ info.id }}</div>
      </div>
    </div>
    <div class="posRecord" v-if="this.$route.name == 'energy_machineIndex'" @click="toUrl('coffee_machine_record', { machine_id: info.id })">收入明细</div>
    <div class="ContenBox ftop" v-if="this.$route.name != 'energy_machineIndex'">
      <div class="entrance" @click="toUrl('coffee_machine_good', { machine_id: info.id })">
        <div class="entrance_top">
          <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2Fcoffee-machine%2F%E7%BB%84%20115%402x.png" alt="" />
        </div>
        <div class="entrance_txt">商品管理</div>
      </div>
      <div class="entrance pos" @click="toUrl('coffee_machine_record', { machine_id: info.id })">
        <div class="entrance_top">
          <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2Fcoffee-machine%2F%E7%BB%84%20217%402x.png" alt="" />
        </div>
        <div class="entrance_txt">收入明细</div>
      </div>
    </div>
    <div :class="[this.$route.name != 'energy_machineIndex' ? 'orderBox' : 'orderBox mt']">
      <div class="title">
        <div class="left">订单数</div>
        <!-- <div class="rigth">
          详情
          <i class="iconfont icon-course_goon"></i>
        </div> -->
      </div>
      <div class="timeSet" @click="orderShow = true">
        <i class="iconfont icon-record_time"></i>
        <span class="timeShow">{{ setTime ? setTime : "请选择日期" }}</span>
        <i class="iconfont icon-adsystem_icon_cancle" @click.stop="orderShowStop"></i>
      </div>
      <div class="orderList">
        <div class="orderList_lis">
          <div class="num">{{ info.today_sale_amount }}</div>
          <div class="txt">今日销售金额</div>
        </div>
        <div class="orderList_lis">
          <div class="num">{{ info.sum_sale_amount }}</div>
          <div class="txt">总销售金额</div>
        </div>
        <div class="orderList_lis" v-if="this.$route.name != 'energy_machineIndex'">
          <div class="num">{{ info.sum_sale_count }}</div>
          <div class="txt">总核销数</div>
        </div>
      </div>
    </div>
    <div class="orderBox">
      <div class="title">
        <div class="left">分润数据</div>
      </div>
      <div class="selectBox">
        <div class="timeSet" @click="bonussShow = true">
          <i class="iconfont icon-record_time"></i>
          <span class="timeShow">{{ setTime_status ? setTime_status : "请选择日期" }}</span>
          <i class="iconfont icon-adsystem_icon_cancle" @click.stop="bonussShowStop"></i>
        </div>
        <div class="statusSet" @click="show = true">
          <span>{{ setStatus }}</span>
          <i class="iconfont icon-icon_down" style="transform: rotate(90deg);"></i>
          <div class="selectStatusBox" v-if="show == true">
            <div class="selectStatusBox_txt" @click.stop="onConfirm('已结算', 0)">已结算</div>
            <div class="selectStatusBox_txt" @click.stop="onConfirm('待结算', 1)">未结算</div>
          </div>
        </div>
      </div>
      <div class="orderList">
        <div class="orderList_lis">
          <div class="num">￥{{ info.today_commission }}</div>
          <div class="txt">今日分润金额</div>
        </div>
        <div class="orderList_lis">
          <div class="num">￥{{ info.sum_commission }}</div>
          <div class="txt">累计分润金额</div>
        </div>
      </div>
      <!-- <div class="line" style="height: 1rem;width: 100%;"></div> -->
    </div>
    <van-popup v-model="bonussShow" position="bottom" :style="{ height: '30%' }">
      <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" @cancel="bonussShow = false" @confirm="bonussShowConfirm" :max-date="maxDate" />
    </van-popup>
    <van-popup v-model="orderShow" position="bottom" :style="{ height: '30%' }">
      <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" @cancel="orderShow = false" @confirm="orderShowConfirm" :max-date="maxDate" />
    </van-popup>
  </div>
</template>
<script>
export default {
  data() {
    return {
      setTime: "",
      setTime_status: "",
      setStatus: "请选择状态",
      // 选择状态
      show: false,
      columns: ["未结算", "已结算"],
      // 订单日期
      orderShow: false,
      // 选择分润日期
      bonussShow: false,
      currentDate: new Date(),
      maxDate: new Date(),

      name: "",
      editShow: false,

      info: []
    };
  },
  activated() {
    console.log(this.$route.name);
    this.getData();
  },
  methods: {
    getData() {
      let json = {
        machine_id: this.$route.params.machine_id,
        order_time: this.setTime,
        commission_time: this.setTime_status,
        commission_status: this.setStatus == "已结算" ? 2 : this.setStatus == "待结算" ? 1 : ""
      };
      let urlstr = this.$route.name == "energy_machineIndex" ? "plugin.energy-machine.frontend.owner.machineDetail" : "plugin.coffee-machine.frontend.owner.machineDetail";
      $http.post(urlstr, json, "loading").then(response => {
        if (response.result == 1) {
          this.name = response.data.name || "咖啡机";
          this.info = response.data;
        } else {
          this.$toast(response.msg);
        }
      });
    },
    editTap() {
      this.editShow = !this.editShow;
      this.$nextTick(() => {
        this.$refs.nameInp.focus();
      });
    },
    blurInp() {
      let urlstr = this.$route.name == "energy_machineIndex" ? "plugin.energy-machine.frontend.owner.rename" : "plugin.coffee-machine.frontend.owner.rename";
      $http.post(urlstr, { machine_id: this.$route.params.machine_id, name: this.name }, "loading").then(response => {
        if (response.result == 1) {
          this.$toast(response.msg);
          this.editShow = false;
        } else {
          this.$toast(response.msg);
          this.editShow = false;
        }
      });
    },
    toUrl(name, params) {
      if (this.$route.name == "energy_machineIndex") {
        this.$router.push(this.fun.getUrl("energy_machine_record", params));
        return;
      }
      this.$router.push(this.fun.getUrl(name, params));
    },
    onConfirm(value, index) {
      this.setStatus = value;
      this.show = false;
      this.getData();
    },
    orderShowConfirm(value) {
      if (this.fun.isIosOrAndroid() == "ios") {
        this.setTime = this.fun.dateFormat("YYYY/mm/dd", value);
      } else {
        this.setTime = this.fun.dateFormat("YYYY-mm-dd", value);
      }
      this.orderShow = false;
      this.getData();
    },
    bonussShowConfirm(value) {
      if (this.fun.isIosOrAndroid() == "ios") {
        this.setTime_status = this.fun.dateFormat("YYYY/mm/dd", value);
      } else {
        this.setTime_status = this.fun.dateFormat("YYYY-mm-dd", value);
      }
      this.bonussShow = false;
      this.getData();
    },
    bonussShowStop() {
      this.setTime_status = "";
      this.getData();
    },
    orderShowStop() {
      this.setTime = "";
      this.getData();
    },
    onCancel() {
      this.show = false;
    }
  }
};
</script>
<style lang="scss" scoped>
.headBox {
  background: linear-gradient(180deg, #28b2fa 0%, #ffffff 100%);
  padding: 1rem 0.75rem;
  padding-bottom: 4rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .left {
    flex: 1;
    .title {
      text-align: left;
      font-size: 1rem;
      color: #ffffff;
      line-height: 1.41rem;
      .icon-fontclass-qianshu {
        margin-left: 0.25rem;
      }
      input {
        border: none;
        width: fit-content;
      }
    }
    .mb {
      margin-bottom: 0.5rem;
    }
    .sm {
      font-size: 0.75rem;
      line-height: 1.03rem;
      color: #ffffff;
    }
  }
  .right {
    flex-shrink: 0;
    .btn {
      padding: 0.25rem 0.75rem;
      border-radius: 0.25rem;
      background: #89d5fd;
      color: #f9f9f9;
    }
  }
}
.mt {
  margin-top: -3rem !important;
}
.posRecord {
  position: fixed;
  top: 8.25rem;
  right: 0;
  background: rgba(0, 0, 0, 0.39);
  font-size: 0.75rem;
  line-height: 1.28rem;
  padding: 0.31rem 0.75rem;
  color: #ffffff;
  border-radius: 0.69rem 0rem 0rem 0.69rem;
}
.orderBox {
  padding: 0.75rem;
  margin: 0 0.75rem;
  margin-top: 0.5rem;
  background: #ffffff;
  border-radius: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-direction: column;
  align-items: flex-start;
  .selectBox {
    display: flex;
    align-items: center;
    .statusSet {
      display: flex;
      align-items: center;
      font-size: 0.75rem;
      color: #28b2fa;
      margin-top: 0.5rem;
      margin-left: 1rem;
      position: relative;
      .selectStatusBox {
        position: absolute;
        left: -0.38rem;
        top: 1.47rem;
        border: 0.0313rem solid #e7e7e7;
        box-shadow: 0rem 0rem 0rem rgba(0, 0, 0, 0.1);
        width: 5.75rem;
        height: 4.55rem;
        border-radius: 0.25rem;
        color: #000;
        font-size: 0.75rem;
        background: #ffffff;
        .selectStatusBox_txt {
          padding: 0.63rem 0;
          margin: 0 1.13rem;
        }
        .selectStatusBox_txt:first-child {
          border-bottom: 1.0016px solid #f0f0f1;
        }
      }
    }
  }
  .timeSet {
    padding-left: 0.5rem;
    margin-top: 0.5rem;
    color: #28b2fa;
    display: flex;
    align-items: center;
    .iconfont {
      font-size: 1rem;
    }
    .timeShow {
      margin: 0 0.25rem;
      font-size: 0.75rem;
    }
  }
  .orderList {
    margin-top: 0.72rem;
    width: 100%;
    box-sizing: border-box;
    overflow: hidden;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .orderList_lis {
      flex: 1;
      flex-direction: column;
      .num {
        font-size: 0.94rem;
        font-weight: bold;
        color: #333333;
        line-height: 1.31rem;
        margin-bottom: 0.25rem;
      }
      .txt {
        font-size: 0.75rem;
        color: #999999;
      }
    }
  }
  .title {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      font-size: 0.94rem;
      font-weight: bold;
      line-height: 1.31rem;
    }
    .rigth {
      font-size: 0.75rem;
      color: #999999;
      .icon-course_goon {
        font-size: 0.5rem;
      }
    }
  }
}
.ContenBox {
  padding: 0.75rem 0;
  margin: 0 0.75rem;
  margin-top: -2.5rem;
  background: #ffffff;
  border-radius: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .entrance {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .entrance_top {
      img {
        width: 2.5rem;
        height: 2.5rem;
      }
    }
  }
  .pos {
    position: relative;
  }
  .pos::after {
    content: "";
    position: absolute;
    left: 0;
    top: 1rem;
    width: 0.0313rem;
    height: 2rem;
    background: #e7e7e7;
  }
}
</style>
